package com.atguigu.tingshu.payment.service.impl;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.payment.PaymentTypeEnum;

import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@SuppressWarnings({"unchecked", "rawtypes"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
    private final PaymentInfoMapper paymentInfoMapper;
    private final KafkaService kafkaService;
    @Autowired
    public PaymentInfoServiceImpl(PaymentInfoMapper paymentInfoMapper, KafkaService kafkaService) {
        this.paymentInfoMapper = paymentInfoMapper;
        this.kafkaService = kafkaService;
    }

    /**
     * @description: 保存交易信息
     * @author Petrichor
     * @date 2024/1/10 16:48
     * @param orderInfo 订单信息
     * @return NULL
     **/
    @Override
    public void savePaymentInfo(OrderInfo orderInfo,String paymentType) {
        log.info("保存交易信息");
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo, orderInfo.getOrderNo());
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(lambdaQueryWrapper);

        if (paymentInfo == null){
            // 用户id
            paymentInfo.setUserId(orderInfo.getUserId());
            // 支付类型：1301-订单 1302-充值
            paymentInfo.setPaymentType(PaymentTypeEnum.getPaymentTypeName(paymentType));
            // 订单号
            paymentInfo.setOrderNo(orderInfo.getOrderNo());
            // 付款方式：1101-微信 1102-支付宝
            paymentInfo.setPayWay(orderInfo.getPayWay());
            // 支付金额
            paymentInfo.setAmount(orderInfo.getOrderAmount());
            // 交易内容
            paymentInfo.setContent(orderInfo.getOrderTitle());
            // 支付状态：1401-未支付 1402-已支付
            // 未支付
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);

            // 保存支付信息
            paymentInfoMapper.insert(paymentInfo) ;
        }
    }
    /**
     * 保存充值订单交易信息
     * @author Petrichor
     * @date 2024/1/10 16:48
     * @param rechargeInfo 充值订单信息
     * @return NULL
     **/
    @Override
    public void savePaymentInfo(RechargeInfo rechargeInfo, String paymentType) {
        log.info("保存交易信息");
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo , rechargeInfo.getOrderNo()) ;
        PaymentInfo paymentInfo = this.getOne(lambdaQueryWrapper);
        if (paymentInfo == null){
            // 用户id
            paymentInfo.setUserId(rechargeInfo.getUserId());
            // 支付类型：1301-订单 1302-充值
            paymentInfo.setPaymentType(PaymentTypeEnum.getPaymentTypeName(paymentType));
            // 订单号
            paymentInfo.setOrderNo(rechargeInfo.getOrderNo());
            // 付款方式：1101-微信 1102-支付宝
            paymentInfo.setPayWay(rechargeInfo.getPayWay());
            // 支付金额
            paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
            // 交易内容
            paymentInfo.setContent("充值");
            // 支付状态：1401-未支付 1402-已支付
            // 未支付
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);

            // 保存支付信息
            paymentInfoMapper.insert(paymentInfo) ;
        }
    }

    /**
     * @description: 更新支付信息数据的支付状态
     * @author Petrichor
     * @date 2024/1/10 20:07
     * @param transaction 支付信息
     **/
    @Override
    public void updatePaymentStatus(Transaction transaction) {
        // 查询支付信息
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo , transaction.getOutTradeNo()) ;
        PaymentInfo paymentInfo = this.getOne(lambdaQueryWrapper);
        // 如果是已支付1402 则直接返回
        if (SystemConstant.PAYMENT_STATUS_PAID.equals(paymentInfo.getPaymentStatus())) {
            return;
        }

        //更新支付信息
        // "支付状态：1401-未支付 1402-已支付
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
        // 订单号
        paymentInfo.setOrderNo(transaction.getOutTradeNo());
        // 交易编号
        paymentInfo.setOutTradeNo(transaction.getTransactionId());
        // 【微信支付订单号】 微信支付系统生成的订单号。
        paymentInfo.setCallbackTime(new Date());
        // 回调信息
        paymentInfo.setCallbackContent(JSON.toJSONString(transaction));
        // 表示交易成功！
        paymentInfoMapper.updateById(paymentInfo);

        // 向MQ中发送更新订单状态的消息
        if(paymentInfo.getPaymentType().equals(SystemConstant.PAYMENT_TYPE_ORDER)) {
            // 向MQ中发送更新订单状态的消息
            kafkaService.sendMsg(KafkaConstant.QUEUE_ORDER_PAY_SUCCESS, paymentInfo.getOrderNo());
        }else {
            // 向MQ中发送更新充值信息状态的消息
            kafkaService.sendMsg(KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS, paymentInfo.getOrderNo());
        }
    }
}
